class Solution {
public:
    int maxHeight(int color1, int color2) {
        int height = 0;
        int color_rest[2];
        color_rest[0] = color1;
        color_rest[1] = color2;
        int rest_idx = 0;
        while (true) {
            if (color_rest[rest_idx] >= height+1) {
                ++height;
                color_rest[rest_idx] -= height;
                rest_idx++;
                rest_idx %= 2;
            } else {
                break;
            }
        }
        return height;
    }

    int maxHeightOfTriangle(int red, int blue) {
        return max(maxHeight(red, blue), maxHeight(blue, red));
    }
};